package com.situ.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.common.ServerResponse;
import com.situ.web.entity.Banji;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.service.IBanjiService;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.BanjiServiceImpl;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JSONUtil;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	
	private IStudentService studentService = new StudentServiceImpl();
	private IBanjiService banjiService = new BanjiServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		req.setCharacterEncoding("utf-8");
		
//		HttpSession session =req.getSession();
//		User user = (User) session.getAttribute("user");
//		if (null == user) {
//			resp.sendRedirect(req.getContextPath() + "/login.jsp");
//			return;
//		}
		
		
		
		String method = req.getParameter("method");
		if (null == method || "".equals(method)) {
			method = "pageList";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "search":
			search(req, resp);
			break;
		case "toUpdate":
			toUpdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "checkName":
			checkName(req, resp);
			break;
		case "getInsertPage":
			getInsertPage(req, resp);
			break;	
		case "selectStudentBanjiInfo":
			selectStudentBanjiInfo(req, resp);
			break;
		default:
			System.out.println("default");
			break;
		}
	}


	private void getInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Banji> list = banjiService.selectAll();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/student_insert.jsp").forward(req, resp);
	}

	private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name =req.getParameter("name");
		boolean isExist = studentService.checkName(name);
		
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write("{\"isExist\" : " + isExist + "}");
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (null == pageNo || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (null == pageSize || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageBean pageBean = studentService.pageList(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
		
		req.setAttribute("pageBean",pageBean);
		req.getRequestDispatcher("student_page_list.jsp").forward(req, resp);
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String updateId = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		Student student = new Student(Integer.parseInt(updateId), name, Integer.parseInt(age), gender);

		boolean isSuccess = studentService.update(student);
		
//		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		ServerResponse serverResponse = null;
		if (isSuccess) {
			serverResponse = ServerResponse.createSuccess("修改成功");
		} else {
			serverResponse = ServerResponse.createSuccess("修改失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
	}

	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentServlet.toUpdate()");
		String updateId = req.getParameter("id");
		Student student = studentService.selectById(Integer.parseInt(updateId));
		System.out.println(student);
		
		req.setAttribute("student", student);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws  IOException, ServletException {
		String searchName = req.getParameter("searchName");

		List<Student> list = studentService.search(searchName);
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
	
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		Student student = new Student(name, Integer.parseInt(age), gender,Integer.parseInt(banjiId));
		
		boolean isSuccess = studentService.insert(student);
		
//		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		
		ServerResponse serverResponse = null;
		if(isSuccess) {
			serverResponse = ServerResponse.createSuccess("添加成功");
		} else {
			serverResponse = ServerResponse.createError("添加失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");

		boolean isSuccess = studentService.deleteById(Integer.parseInt(id));
		// 删除完之后重定向到列表界面再显示所有数据
//		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		
		ServerResponse serverResponse = null;
		if (isSuccess) {
			serverResponse = ServerResponse.createSuccess("删除成功");
		} else {
			serverResponse = ServerResponse.createError("删除失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Student>  list = studentService.selectAll();
		
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
	
	private void selectStudentBanjiInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Map<String, Object>> list = studentService.selectStudentBanjiInfo();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/student_banji_list.jsp").forward(req, resp);
	}
	
}
